/*
  Labotronik EthernetWifi Switch
  Copyright (C) 2012 Marc Chapdelaine

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <iostream>
#include <QtGui>

using namespace std;

#include "MainWindow.hpp"
#include "TftpClient.hpp"

#include <Windows.h>

MainWindow::MainWindow() :
	menuBar(NULL),
	fileMenu(NULL),
	helpMenu(NULL),
	labotronikLogo(NULL),
	labotronikLogoLabel(NULL),
	ipLabel(NULL),
	ipAddressField(NULL),
	gridGroupBox(NULL),
	exitAction(NULL),
	helpAction(NULL),
	aboutAction(NULL),
	ethernetButton(NULL),
	wifiButton(NULL) {
		
	exitAction = new QAction(tr("&Quit"), this);
	helpAction = new QAction(tr("&Help"), this);
	aboutAction = new QAction(tr("&About"), this);
	
	connect(exitAction, SIGNAL(triggered()), this, SLOT(quit()));
	connect(helpAction, SIGNAL(triggered()), this, SLOT(displayHelp()));
	connect(aboutAction, SIGNAL(triggered()), this, SLOT(displayAbout()));
	
	createMenu();
	createGridGroupBox();
	
	
	QVBoxLayout* mainLayout = new QVBoxLayout;
	if(menuBar != NULL)
		mainLayout->setMenuBar(menuBar);
	
	mainLayout->addWidget(gridGroupBox);
	
	setLayout(mainLayout);
	
	QString windowTitle = QString(tr("Labotronik EthernetWifi Switch ")) + QString::number(SWITCHER_VERSION);
	setWindowTitle(windowTitle);
}

void MainWindow::createMenu() {
	//cout << "Creating Menu\n" << endl;
	menuBar = new QMenuBar();
	fileMenu = new QMenu(tr("&File"), this);
	helpMenu = new QMenu(tr("&Help"), this);
	
	if(exitAction != NULL) {
		fileMenu->addAction(exitAction);
	}
	
	if(helpAction != NULL && aboutAction != NULL) {
		helpMenu->addAction(helpAction);
		helpMenu->addSeparator();
		helpMenu->addAction(aboutAction);
	}
	
	menuBar->addMenu(fileMenu);
	menuBar->addMenu(helpMenu);
}


void MainWindow::createGridGroupBox() {
	gridGroupBox = new QGroupBox(tr("Ethernet Wifi Changer"));
	QGridLayout* layout = new QGridLayout;
	
	labotronikLogo = new QPixmap("images/logo.jpg");
	labotronikLogoLabel = new QLabel();
	labotronikLogoLabel->setPixmap(*labotronikLogo);
	
	ethernetButton = new QRadioButton("Ethernet");
	wifiButton = new QRadioButton("Wifi");
	ethernetButton->setChecked(false);
	wifiButton->setChecked(false);
	
	connect(ethernetButton, SIGNAL(toggled(bool)), this, SLOT(switchToEthernet(bool)));
	connect(wifiButton, SIGNAL(toggled(bool)), this, SLOT(switchToWifi(bool)));
	
	ipLabel = new QLabel(QString("Board IP Address: "));
	ipAddressField = new QLineEdit();
	
	layout->addWidget(labotronikLogoLabel, 0, 0);
	layout->addWidget(ethernetButton, 1, 0);
	layout->addWidget(wifiButton, 1, 1);
	layout->addWidget(ipLabel, 2, 0);
	layout->addWidget(ipAddressField, 2, 1);
	
	gridGroupBox->setLayout(layout);	
}

MainWindow::~MainWindow() {

	
}

void MainWindow::switchToEthernet(bool enabled) {
	if(enabled == false)
		return;
	QMessageBox aboutMessage;
	aboutMessage.setText("Debug: SwitchToEthenet Action\n");
	aboutMessage.exec();
}

void MainWindow::switchToWifi(bool enabled) {
	if(enabled == false)
		return;
		
	cout << "test\n";
	QMessageBox aboutMessage;
	Tftp::TftpClient sendWifiFile(QString("192.168.1.78"), 69, this);
	
	sendWifiFile.show();
	
	bool result = sendWifiFile.put(QString("main.cpp"), QString("main.cpp"));
	
	Sleep(1000);
	
	if(result == true)
		aboutMessage.setText("Debug: SwitchToWifi Action success\n");
	else
		aboutMessage.setText("Debug: SwitchToWifi Action fail\n");
	aboutMessage.exec();
}

void MainWindow::quit() {
	close();
}

void MainWindow::displayHelp() {

}


// void MainWindow::createStatusBar() {
	// statusBar()->showMessage(tr("Not Connected"));
// }

void MainWindow::displayAbout() {
	QString version = QString::number(SWITCHER_VERSION);
	QMessageBox aboutMessage;
	QString messageText = QString(tr("Labotronik EthernetWifi Switch ")) + version + QString(tr("\n\nBy: Marc Chapdelaine"));
	aboutMessage.setText(messageText);
	aboutMessage.exec();
}
